package model;

import java.util.ArrayList;
public class CustomerContainer 
{
    public static CustomerContainer instance;
    private ArrayList<Customer> customerList;
    
    public CustomerContainer()
    {
        customerList = new ArrayList<Customer>();
    }
    
    public static CustomerContainer getInstance()
    {
        if(instance == null)
        {
            instance = new CustomerContainer();
        }
        return instance;
    }
    public void addCustomer(Customer customer)
    {
        customerList.add(customer);
    }
    
    
    public ArrayList<Customer> findCustomer(String name) 
    {
        ArrayList<Customer> cList = new ArrayList<Customer>();
        for(Customer c : customerList)
        {
            if(c.getName().equals(name))
            {
                cList.add(c);
            }
        }
        return cList;
    }
    
    public Customer getCustomer(String ID)
    {
        for(int i = 0; i < customerList.size();i++)
        {
            if(customerList.get(i).getID().equals(ID))
            {
                return customerList.get(i);
            }
        }
        return null;
    }
        
    public boolean deleteCustomer(String name)
    {
        if(findCustomer(name)!= null)
        {
            for(int i = 0; i < customerList.size();i++)
            {
                if(customerList.get(i).getName().equals(name))
                {
                    customerList.remove(i);
                    return true;
                }
            }
            return false;
        }
        return false;
    }
    public boolean updateCustomer(String id,String name, String address, String phone, String groupName)
    {
         GroupContainer gc = GroupContainer.getInstance();
        Group g = gc.getGroup(groupName);
        if(g!= null)
        {
            Customer customer = getCustomer(id);
            customerList.get(customerList.indexOf(customer)).setName(name);
            customerList.get(customerList.indexOf(customer)).setAddress(address);
            customerList.get(customerList.indexOf(customer)).setPhone(phone);
            customerList.get(customerList.indexOf(customer)).setGroup(g);
            return true;
        }
        else
        {
            return false;
        }
    }
}
